import { Injectable } from "@nestjs/common";

import { PrismaService } from "@/core/db/prisma.service";
import BusinessException from "@/core/exception/BusinessException";
import { PageResultVO } from "@/core/vo/page-result.vo";

import { CreateHotlistDto } from "./dto/create-hotlist.dto";
import { QueryHotlistDto } from "./dto/query-hotlist.dto";
import { UpdateHotlistDto } from "./dto/update-hotlist.dto";

@Injectable()
export class HotlistService {
    constructor(private readonly prismaService: PrismaService) {}

    create(createHotlistDto: CreateHotlistDto) {
        return this.prismaService.hotList.create({
            data: createHotlistDto
        });
    }

    findAll() {
        return this.prismaService.hotList.findMany({ where: { isDeleted: false } });
    }

    async findPage(query: QueryHotlistDto) {
        const { current, size, title } = query;
        const [listData, total] = await Promise.all([
            this.prismaService.hotList.findMany({
                where: {
                    isDeleted: false,
                    title: {
                        contains: title
                    }
                },
                skip: (current - 1) * size,
                take: size,
                orderBy: {
                    hotScore: "asc"
                }
            }),
            this.prismaService.hotList.count({
                where: {
                    isDeleted: false,
                    title: {
                        contains: title
                    }
                }
            })
        ]);

        const pages = Math.ceil(total / size);
        return new PageResultVO(listData, total, pages, current, size);
    }

    findOne(id: number) {
        return this.prismaService.hotList.findUnique({
            where: { id, isDeleted: false }
        });
    }

    async update(id: number, updateHotlistDto: UpdateHotlistDto) {
        let res;
        try {
            res = await this.prismaService.hotList.update({
                where: {
                    id,
                    isDeleted: false
                },
                data: updateHotlistDto
            });
        } catch (error) {
            throw new BusinessException();
        }
        return res;
    }

    async remove(id: number, softDelete: boolean = true) {
        try {
            if (softDelete) {
                await this.prismaService.hotList.update({
                    where: {
                        id,
                        isDeleted: false
                    },
                    data: {
                        isDeleted: true,
                        deletedAt: new Date()
                    }
                });
            } else {
                await this.prismaService.hotList.delete({
                    where: {
                        id,
                        isDeleted: false
                    }
                });
            }
        } catch (error) {
            throw new BusinessException();
        }
    }
}
